package com.codeczx.notificationdemo;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.IntDef;
import android.support.v7.app.NotificationCompat;
import android.util.Log;

public class DownloadService extends Service {
    public DownloadService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("codeczx","DownloadService onCreate");
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("codeczx","DownloadService onStartCommand");
        createNotification();

        return START_STICKY;
    }

    public void createNotification(){
        final int id = 1;
        final NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        Intent intentClick = new Intent(this, NotificationBroadcastReceiver.class);
        intentClick.setAction("notification_clicked");
        intentClick.putExtra(NotificationBroadcastReceiver.TYPE, 1);
        final PendingIntent pendingIntentClick = PendingIntent.getBroadcast(this, 0, intentClick, PendingIntent.FLAG_CANCEL_CURRENT);

        Intent intentCancel = new Intent(this, NotificationBroadcastReceiver.class);
        intentCancel.setAction("notification_cancelled");
        intentCancel.putExtra(NotificationBroadcastReceiver.TYPE, 2);
        final PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(this, 0, intentCancel, PendingIntent.FLAG_ONE_SHOT);

        final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
        Notification notification = mBuilder.setContentTitle("title")
                .setContentText("content")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true)
                .setOngoing(true)
                .setContentIntent(pendingIntentClick)
                .setDeleteIntent(pendingIntentCancel)
                .build();
        this.startForeground(id,notification);

        new Thread(new Runnable() {
            @Override
            public void run() {
                int incr;
                for(incr=0;incr<=20;incr+=1){
                    Log.i("codeczx","DownloadService incr:"+incr);
                    mBuilder.setProgress(20,incr,false);
                    mNotifyManager.notify(id,mBuilder.build());

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                mBuilder.setContentText("Download complete");
                mNotifyManager.notify(id,mBuilder.build());
            }
        }).start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("codeczx","DownloadService onDestroy");
    }

}
